package com.tuananh.radnom.gwt.client.overall;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.Style.LayoutRegion;
import com.sencha.gxt.explorer.client.model.Example.Detail;
import com.sencha.gxt.state.client.BorderLayoutStateHandler;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.FlowLayoutContainer;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.info.Info;
import com.tuananh.radnom.gwt.client.GreetingService;
import com.tuananh.radnom.gwt.client.GreetingServiceAsync;
import com.tuananh.radnom.gwt.shared.dto.*;
import com.tuananh.radnom.gwt.shared.event.NetNodeTreeEvent;
import com.tuananh.radnom.gwt.shared.event.NetNodeTreeEventHandler;

public class SystemOverall extends BorderLayoutContainer {
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	private static SystemOverallUiBinder uiBinder = GWT
			.create(SystemOverallUiBinder.class);
	HandlerManager eventBus;

	interface SystemOverallUiBinder extends UiBinder<Widget, SystemOverall> {
	}

	public SystemOverall() {
		eventBus = new HandlerManager(null);
		Widget w = uiBinder.createAndBindUi(this);
		this.setWidget(w);
		monitorWindowResize = true;
		Window.enableScrolling(true);
	}
	@Override
	protected void onWindowResize(int width, int height) {
		// TODO Auto-generated method stub
		setPixelSize(Window.getClientWidth(), Window.getClientHeight());
	}
	
	@UiField
	BorderLayoutContainer con;
	@UiField
	NetNodeTree nodeTree;
	@UiField
	NetworkCanvas networkCanvas;
	@UiField
	NodeReport nodeReport;

	@UiFactory
	public NetworkCanvas createNetworkCanvas() {
		greetingService.getAllNode(new AsyncCallback<ArrayList<NetNodeDTO>>() {

			public void onSuccess(final ArrayList<NetNodeDTO> listNode) {
				// TODO Auto-generated method stub
				greetingService
						.getAllConn(new AsyncCallback<ArrayList<NetConnDTO>>() {

							public void onSuccess(ArrayList<NetConnDTO> result) {
								// TODO Auto-generated method stub
								networkCanvas.updateStore(listNode, result);
							}

							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}
						});
			}

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});
		return new NetworkCanvas();
	}

	@UiFactory
	public NetNodeTree createNetNodeTree() {

		greetingService.getAllNode(new AsyncCallback<ArrayList<NetNodeDTO>>() {

			public void onSuccess(final ArrayList<NetNodeDTO> result) {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new NetNodeTreeEvent(
						NetNodeTreeEvent.NodeTreeEventType.FETCH, result, null,
						null));
				//nodeTree.updateStore(result);
			}

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});
		return new NetNodeTree(eventBus);
	}

	@UiFactory
	public FlexTable createFlexTable() {
		FlexTable table = new FlexTable();
		table.getElement().getStyle().setProperty("margin", "10px");
		table.setCellSpacing(8);
		table.setCellPadding(4);

		for (int i = 0; i < LayoutRegion.values().length; i++) {
			final LayoutRegion r = LayoutRegion.values()[i];
			if (r == LayoutRegion.CENTER) {
				continue;
			}

			SelectHandler handler = new SelectHandler() {

				public void onSelect(SelectEvent event) {
					TextButton btn = (TextButton) event.getSource();
					String txt = btn.getText();
					if (txt.equals("Expand")) {
						con.expand(r);
					} else if (txt.equals("Collapse")) {
						con.collapse(r);
					} else if (txt.equals("Show")) {
						con.show(r);
					} else {
						con.hide(r);
					}
				}
			};

			table.setHTML(i, 0, "<div style='font-size: 12px; width: 100px'>"
					+ r.name() + ":</span>");
			table.setWidget(i, 1, new TextButton("Expand", handler));
			table.setWidget(i, 2, new TextButton("Collapse", handler));
			table.setWidget(i, 3, new TextButton("Show", handler));
			table.setWidget(i, 4, new TextButton("Hide", handler));
		}
		return table;
	}
	
	@UiFactory
	public NodeReport createNodeReport(){
		return new NodeReport(eventBus);
	}
}
